package com.travel.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.travel.Bean.Manager;
import com.travel.Config.Security_PasswordEncoder;
import com.travel.Dao.ManagerMapper;
import com.travel.JPA.ManagerJPA;
import com.travel.JPA.UserJPA;
import com.travel.util.AES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class ManagerServiceImpl implements UserDetailsService {

    @Autowired
    private ManagerJPA managerJPA;
    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private UserJPA userJPA;

    /**
     * 加载用户用户名
     *
     * @param username 用户名
     * @return {@link UserDetails}
     * @throws UsernameNotFoundException 用户名没有发现异常
     */
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        Manager user = managerJPA.findByUsername(username);
        com.travel.Bean.User user1;
        if (user == null) {
            user1 = userJPA.findByUsername(username);

            if(user1 == null)
                throw new UsernameNotFoundException("User" + username + "在数据库中找不到");
            else {
                user = new Manager();
                user.setUsername(user1.getUserid());
                user.setPassword(user1.getPassword());
                user.setRole(user1.getRole());
            }

        }


//        下面的注释是单个添加用户权限的方式
        Collection<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(user.getRole());
        grantedAuthorities.add(grantedAuthority);

        //        通过getAuthority()获取用户权限的为List<SimpleGrantedAuthority>形式，添加用户权限
        return new User(username, user.getPassword(), grantedAuthorities);
    }


    /**
     * 找到管理员通过用户名
     *
     * @param username 用户名
     * @return {@link Manager}
     */
    public Manager findManagerByUsername(String username){
        return managerJPA.findByUsername(username);
    }

    /**
     * 找到管理员列表
     *
     * @param page        页面
     * @param limit       记录条数
     * @param adminName   管理员名称
     * @param cultureName 景区名字
     * @return {@link PageInfo}<{@link Manager}>
     */
    public PageInfo<Manager> findManagerList(Integer page,Integer limit,String adminName,String cultureName){

        PageInfo<Manager> managerPageInfo = new PageInfo<>();

        PageHelper.startPage(page,limit);
        List<Manager> managers = managerMapper.selManagerList(adminName,cultureName);
        managerPageInfo.setList(managers);

        return managerPageInfo;
    }


    /**
     * 更新meneger编辑
     *
     * @param manager 经理
     * @return {@link Integer}
     * @throws Exception 异常
     */
    public Integer updateManagerEdit(Manager manager) throws Exception {
        if(manager.getPassword() != null && !manager.getPassword().equals("")){
            manager.setPassword(AES.encrypt(manager.getPassword(), Security_PasswordEncoder.secretKey));
        }else
            manager.setPassword(null);
        try{
            managerMapper.updateManagerEdit(manager);
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }



        return 0;
    }

    @Transactional
    public Integer deleteManagerById(Integer id){
        return managerJPA.deleteByid(id);
    }


    /**
     * 得到景区id通过管理员名字
     *
     * @param managerName 管理员名字
     * @return {@link Integer}
     */
    public Integer getCultureIdByManagerName(String managerName){
        Integer res = managerJPA.findCultureIdByManagerNameAnd(managerName);
        if(res != null){
            return res;
        }
        return 0;
    }

    /**
     * 得到管理员通过景区id
     *
     * @param culture_id 景区id
     * @param page       页面
     * @param limit      记录条数
     * @return {@link PageInfo}<{@link Manager}>
     */
    public PageInfo<Manager> getManagerByCultureId(Integer culture_id,Integer page,Integer limit){
        PageInfo<Manager> managerPageInfo = new PageInfo<>();
        PageHelper.startPage(page,limit);
        List<Manager> managers = managerJPA.findManagersByCulture_id(culture_id);
        managerPageInfo.setList(managers);
        return managerPageInfo;
    }


}
